function varargout = MMOutSel(varargin)
% MMOUTSEL M-file for MMOutSel.fig
%      MMOUTSEL, by itself, creates a new MMOUTSEL or raises the existing
%      singleton*.
%
%      H = MMOUTSEL returns the handle to a new MMOUTSEL or the handle to
%      the existing singleton*.
%
%      MMOUTSEL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MMOUTSEL.M with the given input arguments.
%
%      MMOUTSEL('Property','Value',...) creates a new MMOUTSEL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MMOutSel_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MMOutSel_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help MMOutSel

% Last Modified by GUIDE v2.5 11-Jul-2010 10:32:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MMOutSel_OpeningFcn, ...
                   'gui_OutputFcn',  @MMOutSel_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before MMOutSel is made visible.
function MMOutSel_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to MMOutSel (see VARARGIN)

% Choose default command line output for MMOutSel
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes MMOutSel wait for user response (see UIRESUME)
% uiwait(handles.wndMMO);


% --- Outputs from this function are returned to the command line.
function varargout = MMOutSel_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes during object creation, after setting all properties.
function lstboxUnSel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lstboxUnSel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
global gValue;
item = length(gValue.dmp_list);
init = false;
for(i=1:item)
    if(~gValue.dmp_list(i).en)
        if(~init)
            init = true;
            str_list{1} = [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name];
        else
            str_list = {str_list{:}, [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name]};
        end
    end
end
if(~init)
    str_list = {'no unselected items'};
end
set(hObject, 'string', str_list);


% --- Executes on selection change in lstboxSel.
function lstboxSel_Callback(hObject, eventdata, handles)
% hObject    handle to lstboxSel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns lstboxSel contents as cell array
%        contents{get(hObject,'Value')} returns selected item from lstboxSel


% --- Executes during object creation, after setting all properties.
function lstboxSel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lstboxSel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
global gValue;
item = length(gValue.dmp_list);
init = false;
for(i=1:item)
    if(gValue.dmp_list(i).en)
        if(~init)
            init = true;
            str_list{1} = [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name];
        else
            str_list = {str_list{:}, [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name]};
         end
    end
end
if(~init)
    str_list = {'no selected items'};
end
set(hObject, 'string', str_list);

% --- Executes on button press in pshbtnAdd.
function pshbtnAdd_Callback(hObject, eventdata, handles)
% hObject    handle to pshbtnAdd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global gValue;
sel_id = get(handles.lstboxUnSel, 'value');
if(isempty(sel_id))
    return;
else
    sel_num = length(sel_id);
    lst_num = length(gValue.dmp_list);
    lst_unsel_str = get(handles.lstboxUnSel, 'string');
    for(sel_idx = 1 : sel_num)
        for(lst_idx = 1: lst_num)
            if(isequal(lst_unsel_str{sel_id(sel_idx)}, [gValue.dmp_list(lst_idx).var_name,'.', gValue.dmp_list(lst_idx).fld_name]))
                gValue.dmp_list(lst_idx).en  = true;
                break;
            end
        end
    end
    set(handles.lstboxUnSel, 'value', 1);
    updateOSelUI(handles);
end

% --- Executes on button press in pshbtnRemove.
function pshbtnRemove_Callback(hObject, eventdata, handles)
% hObject    handle to pshbtnRemove (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global gValue;
unsel_id = get(handles.lstboxSel, 'value');
if(isempty(unsel_id))
    return;
else
    unsel_num = length(unsel_id);
    lst_num = length(gValue.dmp_list);
    lst_sel_str = get(handles.lstboxSel, 'string');
    for(unsel_idx = 1 : unsel_num)
        for(lst_idx = 1: lst_num)
            if(isequal(lst_sel_str{unsel_id(unsel_idx)}, [gValue.dmp_list(lst_idx).var_name,'.', gValue.dmp_list(lst_idx).fld_name]))
                gValue.dmp_list(lst_idx).en  = false;
                break;
            end
        end
    end
    set(handles.lstboxSel, 'value', 1);
    updateOSelUI(handles);
end


% --- Executes on button press in pshbtnMMOClose.
function pshbtnMMOClose_Callback(hObject, eventdata, handles)
% hObject    handle to pshbtnMMOClose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global gValue;
close(handles.wndMMO);
mmrun('updateGUI', gValue.main_handles);


% --- Executes on selection change in lstboxUnSel.
function lstboxUnSel_Callback(hObject, eventdata, handles)
% hObject    handle to lstboxUnSel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns lstboxUnSel contents as cell array
%        contents{get(hObject,'Value')} returns selected item from lstboxUnSel

function     updateOSelUI(handles)
global gValue;
item = length(gValue.dmp_list);
init_sel = false;
init_unsel = false;
for(i=1:item)
    if(gValue.dmp_list(i).en)
        if(~init_sel)
            init_sel = true;
            str_sel_list{1} = [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name];
        else
            str_sel_list = {str_sel_list{:}, [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name]};
        end
    else
        if(~init_unsel)
            init_unsel = true;
            str_unsel_list{1} = [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name];
        else
            str_unsel_list = {str_unsel_list{:}, [gValue.dmp_list(i).var_name,'.', gValue.dmp_list(i).fld_name]};
        end
        
    end
end
if(~init_sel)
    str_sel_list = {'no selected items'};
end
if(~init_unsel)
    str_unsel_list = {'no unselected items'};
end
set(handles.lstboxUnSel, 'string', str_unsel_list);
set(handles.lstboxSel, 'string', str_sel_list);

